package com.blb.demo1;

import com.blb.*;

import java.io.IOException;
import java.util.Properties;

/**
 * @author lvtian
 * @date 2022/12/22 10:29
 * 使用工厂模式创建计算机对象
 */
public class ComputerFactory {

    public static Computer makeComputer(){
        //读取properties配置文件
        Properties properties = new Properties();
        try {
            properties.load(ComputerFactory.class.getClassLoader().getResourceAsStream("computer.properties"));
            String brand = properties.getProperty("computer.brand");
            String cpu = properties.getProperty("computer.cpu");
            String memory = properties.getProperty("computer.memory");
            //根据配置创建计算机对象，设置属性
            Computer computer = new Computer();
            computer.setBrand(brand);
            switch (cpu){
                case "Intel":
                    computer.setCpu(new InterCpu());
                    break;
                case "AMD":
                    computer.setCpu(new AMDCpu());
                    break;
            }
            switch (memory){
                case "Kingston":
                    computer.setMemory(new KingStonMemory());
                    break;
                case "S":
                    computer.setMemory(new SMemory());
                    break;
            }
            //返回对象
            return computer;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
